package com.example.reactordemo.service;

import com.example.reactordemo.domain.Student;
import com.example.reactordemo.mapper.SpringWebFluxMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.reactive.TransactionalOperator;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;

@Service
public class SpringWebFluxServiceImpl implements SpringWebFluxService {

    @Resource
    SpringWebFluxMapper springWebFluxMapper;

    @Autowired
    private TransactionalOperator transactionalOperator;

    //查询
    @Override
    public Flux<Student> findAll() {
        return springWebFluxMapper.findAll();
    }

    //新增
    @Override
    public Mono<Integer> insertDefined(Student student) {
//        springWebFluxMapper.insertDefined(student)
        return springWebFluxMapper.updateDefined(9L, "lishi3")
                .then(Mono.error(new RuntimeException("this is exception!")))
                .then(springWebFluxMapper.insertDefined(student))
                .as(transactionalOperator::transactional);
    }
}

